//
//  SLNavigationController.m
//  ShunLianPower2
//
//  Created by 陈镇 on 2017/10/14.
//  Copyright © 2017年 陈镇. All rights reserved.
//

#import "SLNavigationController.h"
#import "SLViewController.h"
@interface SLNavigationController ()<UIGestureRecognizerDelegate>
/** 自定义侧滑手势 */
@property(nonatomic,strong) UIPanGestureRecognizer *panGestureRecognizer;
@end

@implementation SLNavigationController

- (void)viewDidLoad {
    [self setCustomGestureRecognizer];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modalPresentationStyle = UIModalPresentationFullScreen;
//    if (@available(iOS 15.0, *)) {
//        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
//        [appearance configureWithOpaqueBackground];
//        appearance.backgroundColor = UIColor.whiteColor;
//        appearance.shadowColor = UIColorMakeWithHex(@"eeeeee");
//        self.navigationBar.standardAppearance = appearance;
//        self.navigationBar.scrollEdgeAppearance = self.navigationBar.standardAppearance;
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCustomGestureRecognizer {
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:target action:handler];

    // 设置手势代理，拦截手势触发
    _panGestureRecognizer.delegate = self;

    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:_panGestureRecognizer];

    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    // ---------------------- return YES------------------------------
    //如果在此处 return YES ,则是全屏侧滑返回
    
    CGPoint location = [gestureRecognizer locationInView:self.view];
    CGPoint offSet   = [gestureRecognizer locationInView:gestureRecognizer.view];
    BOOL    result   = (0 < offSet.x && location.x <= 40);
    if (result) {//给自己定义的手势 加一个个开关
        UIViewController *topViewController = self.childViewControllers.lastObject;
        if ([topViewController respondsToSelector:@selector(isUsePanGestureRecognizer)]) {
            if (![topViewController isUsePanGestureRecognizer]) {
                return NO;
            }
        }
    }
    
    if (result) {
        // 兼容 qmui的侧滑返回
        SLViewController *topViewController = self.childViewControllers.lastObject;
        if ([topViewController respondsToSelector:@selector(shouldPopViewControllerByBackButtonOrPopGesture:)]) {
            return [topViewController shouldPopViewControllerByBackButtonOrPopGesture:YES];
        }
    }
    return result;
}

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    [super dismissViewControllerAnimated:flag completion:completion];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}

@end
@implementation UIViewController (BackButtonHandler)


@end

